package cn.jiyun.service;

import cn.jiyun.entity.ResultClass;
import cn.jiyun.entity.day11.TestPaper;
import cn.jiyun.entity.day11.TestPaperVO;
import cn.jiyun.entity.day12.ContestVO;
import cn.jiyun.entity.day15.Train;
import cn.jiyun.entity.day15.TrainDTO;
import cn.jiyun.entity.day9.Course;
import cn.jiyun.entity.day9.CourseVO;
import cn.jiyun.service.fegin.CourseFeginService;
import cn.jiyun.service.fegin.DirectoryFeginService;
import cn.jiyun.service.fegin.TestPaperFeginService;
import cn.jiyun.service.fegin.TrainFeginService;
import com.alibaba.cloud.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author sjt
 * @Date 2023/8/31 11:42
 * @PackageName: cn.jiyun.service.fegin.EightDayService
 */
@Service
public class DayEightService {
    @Autowired
    TrainFeginService trainFeginService;
    @Autowired
    CourseFeginService courseFeginService;
    @Autowired
    TestPaperFeginService testPaperFeginService;
    @Autowired
    DirectoryFeginService directoryFeginService;
    @Autowired
    RedisTemplate redisTemplate;

//分页查询培训集合
    public ResultClass findTrainList(TrainDTO dto) {
        return trainFeginService.findTrainList(dto);
    }
//查询是否参加此培训
    public ResultClass findUserIfParticipate(Integer uid, Integer tid) {
        return trainFeginService.findUserIfParticipate(uid,tid);
    }
//参加培训
    public ResultClass participateTrain(Integer uid, Integer tid) {
        boolean b=trainFeginService.participateTrain(uid,tid);
        if(b)return ResultClass.ok(b).Message("参加成功");
        return ResultClass.error().Message("参加失败");
    }
//查询竞赛的课程集合信息
    public ResultClass findTrainCourseList(Integer tid,Integer uid) {
        List<Integer> cids=trainFeginService.findTrainCourseIds(tid);
        List<CourseVO> contestVOList=new ArrayList<>();
        for (Integer cid : cids) {
//            查询一个课程
            CourseVO vo = new CourseVO();
            BeanUtils.copyProperties(courseFeginService.selectOne(String.valueOf(cid)),vo);
//            查询用户学习课程的进度
            Integer schedule=directoryFeginService.findUserCourseSchedule(cid,uid);
            vo.setSchedule(schedule);
//            查询课程学习人数
            Integer xueCount=trainFeginService.findCourseXueCount(cid);
            vo.setXueNum(xueCount);
//            计算课程点赞人数
            String zan = String.valueOf(redisTemplate.opsForHash().get("课程点赞", String.valueOf(vo.getCid())));
            int num=0;
            if(StringUtils.isNotBlank(zan)){
                String[] split = zan.split("uid:");
                num=split.length-1;
            }
            vo.setZan(zan);
            vo.setZanNum(num);
//            添加入集合
            contestVOList.add(vo);
        }
        return ResultClass.ok(contestVOList);
    }

    public ResultClass findTrainTestPaperList(Integer tid) {
        List<Integer> arr=trainFeginService.findTestPaperIds(tid);
        List<TestPaperVO> paperVOList=new ArrayList<>();
        for (Integer pid : arr) {
            TestPaperVO vo = new TestPaperVO();
            //查询试卷
            TestPaper testPaper=testPaperFeginService.selectOne(pid);
            BeanUtils.copyProperties(testPaper,vo);
            //查询试卷试题个数
            Integer topicNum=testPaperFeginService.findTestPaperTopicNum(pid);
            vo.setTopicNum(topicNum);
            //查询试卷的完成人数
            Integer overNum=testPaperFeginService.selectTestPaperOverNum(pid);
            vo.setOverNum(overNum);

            paperVOList.add(vo);
        }
        return ResultClass.ok(paperVOList);
    }
}
